package com.example.chat.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.Admin;
import com.example.chat.beans.Order;
import com.example.chat.mapper.AdminMapper;
import com.example.chat.mapper.OrderMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.Base;
import com.example.chat.result.ListResult;
import com.example.chat.result.OrderResult;
import com.example.chat.utils.CheckUtils;
import com.example.chat.utils.Global;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {
    @Resource
    CheckUtils checkUtils;
    @Resource
    AdminMapper adminMapper;
    @Resource
    AdminService adminService;

    public ListResult<List<OrderResult>> listOrder(Integer userId, String token, String tel,Integer isMine, Integer page, Integer limit) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "1301");
        if (apiResult.getStatus() != 1)
            return new ListResult<>(apiResult.getStatus(), apiResult.getMsg(), null, 0, 0, 0);
        ListResult<List<OrderResult>> result = new ListResult<>();
        int status = 0;
        String msg = "获取失败";
        try {
            Page<Order> pages = new Page<>(page, limit);
            IPage<Order> iPages = baseMapper.selectBySearch(pages, tel,isMine,userId);
            List<OrderResult> orderResults = new ArrayList<>();
            for (Order order : iPages.getRecords()) {
                OrderResult orderResult = new OrderResult();
                orderResult.setId(order.getId());
                orderResult.setPhone(order.getPhone());
                orderResult.setCreateTime(Global.longToString(order.getCreateTime()));
                orderResult.setCreateAdmin(order.getCreateAdmin());
                orderResult.setCreateAdminName(order.getCreateAdminName());
                orderResult.setNumber(order.getNumber());
                orderResult.setMoney(order.getMoney());
                orderResult.setStatus(order.getStatus());
                orderResults.add(orderResult);
            }
            result.setData(orderResults);
            result.setPage(page);
            result.setLimit(limit);
            result.setTotal(iPages.getTotal());
            msg = "获取成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public String dealSuccessPay(String number, String subject, String tradeStatus) {
        Order order = baseMapper.selectByNumber(number);
        if (order == null) {
            return "fail";
        }
        if (!tradeStatus.equals("TRADE_SUCCESS")) {
            if (tradeStatus.equals("TRADE_CLOSED")) {
                order.setStatus(3);
                baseMapper.updateById(order);
            }
            return ("fail");
        }
        order.setStatus(2);
        baseMapper.updateById(order);

        Admin admin = adminMapper.selectById(order.getCreateAdmin());
        if (admin == null) {
            return "fail";
        }
        adminService.updateBySuccessPay(admin, subject);
        return "success";
    }
}
